using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectLoaiGia()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA
			};
			return View(CreateViewModel(data));
		}

		public ActionResult SelectLoaiGiaPaging(int page = 1)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiGia", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteLoaiGia(int id)
		{
			return Json(new{
				Success = _repLoaiGia.Delete(id),
				RecordCount = _repLoaiGia.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult EditLoaiGia(int page, int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA,
				CurrentPage = page,
				EditID = id
			};
			return PartialView("Templates/TH_EditLoaiGia", CreateViewModel(data));
		}

		public ActionResult CancelEditingLoaiGia(int page)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA,
				CurrentPage = page
			};
			return PartialView("Templates/TH_ListLoaiGia", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateLoaiGia(FormCollection forms)
		{
			string id = forms.GetValues("LoaiGia_ID")[0];
			string tenloaigia = forms.GetValues("LoaiGia_TenLoaiGia")[0];
			string currentpage = forms.GetValues("LoaiGia_CurrentPage")[0];
			var loaigia = _repLoaiGia.SelectByID(int.Parse(id));
			loaigia.TenLoaiGia = tenloaigia;
			var result = _repLoaiGia.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA,
				CurrentPage = int.Parse(currentpage)
			};
			return PartialView("Templates/TH_ListLoaiGia", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertLoaiGia(FormCollection forms)
		{
			string tenloaigia = forms.GetValues("LoaiGia_TenLoaiGia")[0];
			var newItem = new LoaiGia
			{
				TenLoaiGia = tenloaigia
			};
			var result = _repLoaiGia.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_LOAIGIA,
				Added = result
			};
			return Json(new{
				Success = result,
				Message = "A new item has been added!",
				PartialViewHtml = RenderPartialViewToString("Templates/TH_ListLoaiGia", CreateViewModel(data))
			});
		}


	}
}